<?php
/**
 * 教师控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Teacher extends Controller_BaseController {

	public $template = 'edu/template';

	public function action_index(){
		$params = array(
  			'title' => '课程中心——教师管理',
  			'menu' => ''
  		);

		$params['items'] = \Model_Teacher::getItems('*', null, array('people'));

  		\View::set_global($params);
		$this->template->content = \View::forge("edu/course/teachers");
	}

	/**
	* 新增一个教师
	*
	* @return
	*/
	public function action_create(){

		if(\Input::method() == 'POST'){
			$val = \Validation::forge('MyRules');
			$val->add_callable('MyRules');
			$val->add_field('email', 'email', 'required|valid_email|unique[users.email]');
			$val->add_field('phone', 'phone number', 'required');

			if (! $val->run()){
				foreach ($val->error() as $key => $value) {
	        		$errors[$key] = (string)$value;
	        		echo $value . '<br>';
	      		}
	      		if(\Input::is_ajax()){
	      			die(json_encode(array('status' => 'err', 'msg' => '表单验证错误', 'data' => $errors, 'errcode' => 10)));
	      		}
	      		\Session::set_flash('msg', array('status' => 'err', 'msg' => '表单验证错误', 'data' => $errors, 'errcode' => 10));
	      		die();
	      		return;
			}

			$data = \Input::post();
			$data['is_delete'] = 0;
			try{
				$id = \Auth::create_user('teacher' . time(), time(), $data['email'] ? $data['email'] : time() . '@test.com', 1);
			}catch(Exception $e){
				die($e->getMessage());
			}
			$data['user_id'] = $id;
			\Model_People::forge($data)->save();

			//新增课程
			$courses = array();
			foreach ($data['courselist'] as $key => $value) {
				if($value == 0){
					continue;
				}
				array_push($courses, \Model_TeacherCourse::forge(array('course_id' => $value)));
			}

			$teacher = \Model_Teacher::forge($data);
			$teacher->courses = $courses;

			if($teacher->save()){
				if(\Input::is_ajax()){
	      			die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
	      		}
				\Response::redirect('/admin/teacher');
			}
		}
		
	}

	/**
	* 编辑教师资料
	*
	* @param $id int 教师ID
	* @return
	*/
	public function action_view($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$teacher = \Model_Teacher::find($id);
		if( ! $teacher){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		if(\Input::is_ajax()){
			$people = $teacher->to_array();
			$people['people'] = $teacher->people->to_array();
			$people['courses'] = array();
			foreach ($teacher->courses as $key => $value) {
				if($value->course){
					array_push($people['courses'], $value->course->to_array());
				}
			}
			die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $people)));
		}

		$params['item'] = $teacher;

  		\View::set_global($params);
		$this->template->content = \View::forge("edu/course/teacher");
	}

	/**
	* 编辑教师资料
	*
	* @param $id int 教师ID
	* @return
	*/
	public function action_edit($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$data = \Input::post();

		$teacher = \Model_Teacher::find($id);
		if( ! $teacher){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		if(\Input::post('courselist', false)){
			//检测移除课程
			foreach ($teacher->courses as $key => $course){
				$flag = false;
				foreach (\Input::post('courselist') as $k => $value) {
					if($value == $course->course_id){
						unset($data['courselist'][$k]);
						$flag = true;
						break;
					}
				}
				if($flag){
					continue;
				}
				unset($teacher->courses[$key]);
			}

			//新增课程
			foreach ($data['courselist'] as $key => $value) {
				array_push($teacher->courses, \Model_TeacherCourse::forge(array('teacher_id' => $teacher->id, 'course_id' => $value)));
			}
		}

		$teacher->people->first_name = $data['first_name'];
		$teacher->people->last_name = $data['last_name'];
		$teacher->people->phone = $data['phone'];
		$teacher->people->gender = $data['gender'];
		$teacher->people->photo = $data['photo'];
		$teacher->people->email = $data['email'];
		
		$teacher->set($data);
		if($teacher->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
			}
			\Response::redirect('/admin/teacher');
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
			}
			\Response::redirect('/admin/teacher');
		}
	}

	/**
	* 删除一个教师
	*
	* @param $id int 教师ID
	* @return
	*/
	public function action_delete($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$teacher = \Model_Teacher::find($id);
		if( ! $teacher){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$teacher->is_delete = true;
		if($teacher->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

}
